package basic;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class MessageDAOUIDJpa {

	public static void main(String[] args) throws InterruptedException {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("messagepu");

		// 插入
		EntityManager em = emf.createEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();

//		Message message = new Message("Hello World with JPA");
		long size = 10;
		long[] arr = new long[(int) size];
		for (int i = 0; i < size; i++) {
			Thread.sleep(1000);
			Message m = em.merge(new Message("Hello World with JPA"));
			
			long suffix = Long.parseLong(m.getId().substring(24, 32),16);
			arr[i] = suffix;
			if(i>0){
				System.out.println(m.getId()+" | "+ (arr[i]-arr[i-1]));  
			}
			else{
				System.out.println(m.getId()+" | 0");  
			}
		}

		tx.commit();
		em.close();


		// Shutting down the application
		emf.close();
	}
}
